package com.rmb.api.controller;

import com.alibaba.fastjson.JSON;
import com.rmb.api.service.MartApiEssentialService;
import com.rmb.common.model.response.*;
import com.rmb.common.validated.AddGroup;
import com.rmb.common.validated.RecordQueryGroup;
import com.rmb.common.validated.UpdateGroup;
import com.rmb.mart.entity.api.*;
import com.rmb.mart.qurey.QueryPage;
import com.rmb.mart.entity.api.MartApiError;
import com.rmb.mart.entity.api.MartApiCharge;
import com.rmb.mart.entity.api.MartApiEssential;
import com.rmb.mart.entity.api.MartApiRule;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (MartApiEssential)表控制层
 *
 * @author makejava
 * @since 2021-01-20 09:37:35
 */
@RestController
@RequestMapping("/rmb/martApiEssential")
public class MartApiEssentialController {
    /**
     * 服务对象
     */
    @Resource
    private MartApiEssentialService martApiEssentialService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne/{apiId}")
    public MartApiEssential selectOne(@PathVariable("apiId") Long id) {
        return this.martApiEssentialService.queryById(id);
    }


    /**
     * 查询接口数据+模糊查询+分页
     *
     * @param martApiEssential 模糊查询
     * @return 对象列表
     */
    @PostMapping("/getAllApi")
    public QueryResponseResult selectAllApi(@RequestBody MartApiEssential martApiEssential){

        return new QueryResponseResult(
                CommonCode.QUERY_SUCCESS,
                martApiEssentialService.queryAllByLimit(martApiEssential));
    }

    /**
     * 后台api详情
     * @param apiId 接口ID
     * @return  详情信息
     */
    @GetMapping("/getApiRecord/{apiId}")
    public QueryResponseResult selectApiRecord(@PathVariable("apiId") Long apiId){

        return new QueryResponseResult(CommonCode.QUERY_SUCCESS,
                new QueryResult<>(
                        martApiEssentialService.selectApiRecord(apiId),
                        martApiEssentialService.selectApiRecord(apiId).size()
                )
        );
    }

    /**
     * 修改Api接口数据
     * @param martApiEssential 实例对象
     * @return 实例对象
     */
    @PostMapping("/updateApiEssential")
    public ResponseResult updateApiEssential(@RequestBody @Validated(UpdateGroup.class) MartApiEssential martApiEssential){

        return martApiEssentialService.updateApiEssential(martApiEssential) > 0 ?
                new ResponseResult(CommonCode.SUCCESS, JSON.toJSONString(martApiEssentialService.queryById(martApiEssential.getId()))):
                new ResponseResult(CommonCode.FAIL);
    }

    /**
     * 根据指定ID删除Api
     * @param apiId 接口ID
     * @return 操作结果
     */
    @GetMapping("/delApiEssentialById/{apiId}")
    public ResponseResult removeApiEssentialById(@PathVariable("apiId")Long apiId){
        return martApiEssentialService.removeApiEssentialById(apiId) > 0 ?
                new ResponseResult(CommonCode.SUCCESS,JSON.toJSONString(martApiEssentialService.queryById(apiId))):
                new ResponseResult(CommonCode.FAIL);
    }

    /**
     * 新增Api接口数据
     * @param martApiEssential 实例对象
     * @return 实例对象
     */
    @PostMapping("/addApiEssential")
    public ResponseResult addApiEssential(@RequestBody @Validated(AddGroup.class) MartApiEssential martApiEssential){

        return martApiEssentialService.addApiEssential(martApiEssential) > 0 ?
                new ResponseResult(CommonCode.SUCCESS, JSON.toJSONString(martApiEssentialService.queryById(martApiEssential.getId()))):
                new ResponseResult(CommonCode.FAIL);
    }

    /**
     * 更改Api状态
     * @param apiId 接口ID
     * @param status Api状态
     * @return 受影响行数
     */
    @GetMapping("/updateApiEssentialStatus/{apiId}/{status}")
    public ResponseResult updateApiStatus(@PathVariable("apiId")Integer apiId,@PathVariable("status")Integer status){

        return martApiEssentialService.updateApiStatus(apiId,status) > 0 ?
                new ResponseResult(CommonCode.SUCCESS):
                new ResponseResult(CommonCode.FAIL);
    }

    /**
     * 接口使用量查询
     * @param martApiRecord
     * @return
     */
    @PostMapping("/getUsageAmount")
    public QueryResponseResult selectUsageAmount(@RequestBody @Validated(RecordQueryGroup.class) MartApiRecord martApiRecord){

        return new QueryResponseResult(CommonCode.QUERY_SUCCESS,
                new QueryResult<>(
                        martApiEssentialService.selectUsageAmount(martApiRecord),
                        martApiEssentialService.selectUsageAmount(martApiRecord).size()
                )
        );
    }
    /**
     * 查询接口规则
     *
     * @param queryPage 分页对象
     * @return 规则集合
     */
    @PostMapping("/getApiRule")
    public QueryResponseResult selectAllRule(@RequestBody QueryPage queryPage){

        return new QueryResponseResult(CommonCode.SUCCESS,null,martApiEssentialService.selectAllRule(queryPage));
    }

    /**
     * 通过主键查询单条接口规则数据
     *
     * @param apiRuleId 接口规则主键
     * @return 单条数据
     */
    @GetMapping("/getApiRuleById/{apiRuleId}")
    public ResponseResult selectApiRuleById(@PathVariable("apiRuleId") Long apiRuleId) {

        MartApiRule martApiRule = this.martApiEssentialService.selectApiRuleById(apiRuleId);
        return martApiRule != null ?
                new ResponseResult(CommonCode.QUERY_SUCCESS, JSON.toJSONString(martApiRule)) :
                new ResponseResult(CommonCode.QUERY_FAIL);
    }
    public MartApiRule selectapirulebyid(@PathVariable("apiRuleId") Long apiRuleId) {
        return this.martApiEssentialService.selectApiRuleById(apiRuleId);
    }

    /**
     * 编辑Api接口规则数据
     * @param martApiRule 实例对象
     * @return 实例对象
     */
    @PostMapping("/updateApiRule")
    public ResponseResult updateApiRule(@RequestBody @Validated(UpdateGroup.class) MartApiRule martApiRule){

        return martApiEssentialService.updateApiRule(martApiRule) > 0 ?
                new ResponseResult(
                        CommonCode.SUCCESS,
                        JSON.toJSONString(martApiEssentialService.selectApiRuleById(martApiRule.getId()))) :
                new ResponseResult(CommonCode.FAIL);
    }

    /**
     * 更改Api规则状态
     * @param apiRuleId 接口规则ID
     * @param status 规则状态
     * @return 受影响行数
     */
    @PutMapping("/updateApiRuleStatus/{apiRuleId}/{status}")
    public ResponseResult updateApiRuleStatus(@PathVariable("apiRuleId")Integer apiRuleId,@PathVariable("status")Integer status){

        return martApiEssentialService.updateApiRuleStatus(apiRuleId,status) > 0 ?
                new ResponseResult(CommonCode.SUCCESS):
                new ResponseResult(CommonCode.FAIL);
    }

    /**
     * 删除Api规则
     * @param apiRuleId 接口规则ID
     * @return 操作结果 成功/失败
     */
    @GetMapping("/delApiRuleById/{apiRuleId}")
    public ResponseResult removeApiRuleById(@PathVariable("apiRuleId")Long apiRuleId){
        return martApiEssentialService.removeApiRuleById(apiRuleId) > 0 ?
                new ResponseResult(CommonCode.SUCCESS,JSON.toJSONString(martApiEssentialService.queryById(apiRuleId))):
                new ResponseResult(CommonCode.FAIL);
    }


    /**
     * 接口计费方式
     * @return 计费数据
     */
    @GetMapping("/getAllCharge")
    public QueryResponseResult selectAllCharge(@RequestBody MartApiCharge martApiCharge){
        return new QueryResponseResult(CommonCode.QUERY_SUCCESS,martApiEssentialService.selectAllCharge(martApiCharge));
    }

    /**
     * 编辑接口计费方式
     * @param martApiCharge 实例对象
     * @return 编辑结果
     */
    @PutMapping("/updateApiCharge")
    public ResponseResult updateApiCharge(@RequestBody @Validated MartApiCharge martApiCharge){
        return martApiEssentialService.updateApiCharge(martApiCharge) > 0 ?
                new ResponseResult(CommonCode.SUCCESS,
                         JSON.toJSONString(martApiEssentialService.selectApiChargeById(martApiCharge.getId()))) :
                new ResponseResult(CommonCode.FAIL) ;
    }

    /**
     * 查询数据统计量
     * 1.当天数据
     * 2.数据总量
     * 3.近15日每天数据量
     * @return 放入list结果集
     */
    @GetMapping("/selectStatistics/{id}")
    public QueryResponseResult selectStatistics(@PathVariable("id") Long id){
        List<Map<String,Object>> list =
                martApiEssentialService.selectStatistics(id);
        return new QueryResponseResult(CommonCode.SUCCESS,list);
    }


    /**
     * 查询Api类型
     * @param queryPage 分页对象
     * @return 查询结果
     */
    @PostMapping("/getAllApiType")
    public QueryResponseResult selectAllApiType(@RequestBody QueryPage queryPage){
        martApiEssentialService.selectAllApiType(queryPage);
        return new QueryResponseResult(null,null,null);
    }

    /**
     * 修改Api类型
     * @param martApiType 实例对象
     * @return 操作结果
     */
    public ResponseResult updateApiType(@RequestBody  @Validated MartApiType martApiType){
        return martApiEssentialService.updateApiType(martApiType) > 0 ?
                new ResponseResult(CommonCode.SUCCESS,JSON.toJSONString(martApiEssentialService.selectApiTypeById(martApiType.getId()))):
                new ResponseResult(CommonCode.FAIL);
    }


    /**
     * 查询错误码参照
     * @return
     */
    @GetMapping("/selectErrorQuery")
    public ResponseResult selectErrorQuery(){
        List<MartApiError> apiErrorList = martApiEssentialService.selectErrorQuery();
        return new ResponseResult(CommonCode.SUCCESS,JSON.toJSONString(apiErrorList));
    }

    /**
     * 根据企业id查询
     * @param id
     * @return
     */
    @GetMapping("/findByCompanyId/{id}")
    public List<MartApiEssential> findByCompanyId(@PathVariable("id")Long id){
        return martApiEssentialService.findByCompanyId(id);
    }
}